package client;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import exception.TrackerException;
import file.File;


public class ClientTracker implements Runnable {

	private Thread _thread;
	private Client _client;
	private Socket _sockToTracker;
	private ClientServer _clientServer;
	
	public ClientTracker(String ipTracker, int port, Client c, ClientServer cs){
		_thread = new Thread(this, "Connection to Tracker");
		_client = c;
		_clientServer = cs;
		/*try {
			_sockToTracker = new Socket(ipTracker, port);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}*/
		_thread.start();
	}
	
	@Override
	public void run() {
		try {
			ConnectionToTracker ct = new ConnectionToTracker(_client, _clientServer);
			while(true){
					Thread.sleep(_client.clientConfig.getTrackerUpdateTime()*1000); 
					ct.updateTracker(_client.getMySeed(), _client.getMyLeech());
					for(File f : _client.getMyLeech()){
						ct.getPeers(f.getIdFile());
					}
			}
		}catch (InterruptedException e) {
			e.printStackTrace();
		} catch (TrackerException e) {
			e.printStackTrace();
		}
	}

}
